import * as getCourseInfoService from '../../services/courseIndex'
import {setcookie} from '../../utils/cookies'
import {getcookie} from '../../utils/cookies'
import {routerRedux} from 'dva/router'
import {EXPIRE_SECONDS} from '../../utils/constants'
import {cetGroupId} from '../../utils/constants'
import {testGroupId} from '../../utils/constants'
import {itGroupId} from '../../utils/constants'


export default {
  namespace: 'courseIndex',
  state: {
    newGroupList:[
      {
        groupName:"", groupId: 1,newGroupName:"",
        courseList:[
          {
            orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
            coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
            commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
            enrollmentEndTime: -1, courseBeginTime:-1,
            courseEndTime:-1, salesNum:-1,
          }]
      },
      {
        groupName:"", groupId: 1,newGroupName:"",
        courseList:[
          {
            orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
            coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
            commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
            enrollmentEndTime: -1, courseBeginTime:-1,
            courseEndTime:-1, salesNum:-1,
          }]
      },
      {
        groupName:"", groupId: 1,newGroupName:"",
        courseList:[
          {
            orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
            coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
            commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
            enrollmentEndTime: -1, courseBeginTime:-1,
            courseEndTime:-1, salesNum:-1,
          }]
      },
    ],
    groupList:[
        {
          groupName:"", groupId: 1,
          courseList:[
            {
              orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
              coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
              commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
              enrollmentEndTime: -1, courseBeginTime:-1,
              courseEndTime:-1, salesNum:-1,
            }]
        },
    ],
      getCourseIndexState:null,swiperList:[{courseImgName:''}]
  },
  reducers: {
    getCourseInfoSave(state,{payload}){
      let newGroupList = payload.groupList;
      newGroupList.map((item)=>{
        let newItem = item
        if (newItem.groupId == cetGroupId) {

          newItem.newGroupName = '我要考四六级'

        }
        if (newItem.groupId == itGroupId) {

          newItem.newGroupName = '我要转IT'

        }
        if (newItem.groupId == testGroupId) {
          newItem.newGroupName = '我要考研'

        }

        return newItem
      })
      newGroupList = newGroupList.filter(item=>item.newGroupName == '我要考研'
        ||item.newGroupName == '我要转IT' || item.newGroupName == '我要考四六级'
      )
      return {
        ...state,groupList:payload.groupList,
        swiperList:payload.swiperList,
        getCourseIndexState:payload.values.getCourseIndexState,
        newGroupList:newGroupList,
      }

    },
    // newGroupListSave(state,{payload}){
    //
    //  let newGroupList = payload.groupList;
    //   newGroupList.map((item)=>{
    //     let newItem = item
    //     if (newItem.groupId == cetGroupId) {
    //
    //       newItem.newGroupName = '我要考四六级'
    //
    //     }
    //     if (newItem.groupId == itGroupId) {
    //
    //       newItem.newGroupName = '我要转IT'
    //
    //     }
    //     if (newItem.groupId == testGroupId) {
    //       newItem.newGroupName = '我要考研'
    //
    //     }
    //
    //     return newItem
    //   })
    //   newGroupList = newGroupList.filter(item=>item.newGroupName == '我要考研'
    //     ||item.newGroupName == '我要转IT' || item.newGroupName == '我要考四六级'
    //   )
    //
    //   return {
    //     ...state,newGroupList:newGroupList
    //   }
    //
    // },
    resetState(state,{payload}){
      return {
        ...state, getCourseIndexState:payload.getCourseIndexState,
      }
    }


  },
  effects: {
    *getCourseIndexInfo({payload:values},{call,put}){
      const {data} = yield call (getCourseInfoService.getCourseIndexInfo,values)
      // console.log("----------data",data)
      yield put({type:"getCourseIndexSave",payload:{data}})
      if (data.success == true) {

        // setcookie("groupList", JSON.stringify(data.data),EXPIRE_SECONDS);
        // console.log("---------data.data",data.data)
        let groupId = data.data[0].groupId
        // setcookie("groupId", groupId,EXPIRE_SECONDS);
        let courseId = data.data[0].courseList[0].courseId

        // setcookie("courseId", courseId,EXPIRE_SECONDS);
        let groupList =  data.data
        groupList.sort((a,b)=>{return a.weight-b.weight})
        let newGroupList = []
        for (let i = 0;i < groupList.length; i++) {
          groupList[i].sortId = i
        }
        if (groupList[0].courseList != undefined) {
          groupList[0].courseList.sort((a,b)=>{return a.weight - b.weight})
        }
        if (groupList[1] != undefined) {
          groupList[1].courseList.sort((a,b)=>{return a.weight - b.weight})
        }
        if (groupList[2] != undefined) {
          groupList[2].courseList.sort((a,b)=>{return a.weight - b.weight})
        }
        if (groupList[3] != undefined) {
          groupList[3].courseList.sort((a,b)=>{return a.weight - b.weight})
        }


        // console.log("-------------------courseId",courseId)
        groupList[0].courseList.splice(0,4)
        for (let i = 0;i < groupList.length;i++) {
          if (i == 0) {
            groupList[0].courseList = groupList[0].courseList.splice(0,4)
            // console.log("---------------groupList[0] --",groupList[0] )
          }
          if (i == 1 && groupList[1] != undefined) {
            groupList[1].courseList = groupList[1].courseList.splice(0,2)
          }



          if (i == 2 && groupList[2] != undefined) {
            groupList[2].courseList = groupList[2].courseList.splice(0,2)
          }



          if (i == 3 && groupList[3] != undefined) {
            groupList[3].courseList = groupList[3].courseList.splice(0,4)
          }


          if (i == groupList.length-1) {
            if (groupList !=undefined) {
              groupList[0] = groupList[0]
            } else {
              groupList[0] = [
                {
                  groupName:"", groupId: 1,
                  courseList:[
                    {
                      orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
                      coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
                      commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
                      enrollmentEndTime: -1, courseBeginTime:-1,
                      courseEndTime:-1, salesNum:-1,
                    }]
                },
              ]
            }


            if (groupList !=undefined) {
              groupList[1] = groupList[1]
            } else {
              groupList[1] = [
                {
                  groupName:"", groupId: 1,
                  courseList:[
                    {
                      orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
                      coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
                      commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
                      enrollmentEndTime: -1, courseBeginTime:-1,
                      courseEndTime:-1, salesNum:-1,
                    }]
                },
              ]
            }

            if (groupList !=undefined) {
              groupList[2] = groupList[2]
            } else {
              groupList[2] = [
                {
                  groupName:"", groupId: 1,
                  courseList:[
                    {
                      orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
                      coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
                      commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
                      enrollmentEndTime: -1, courseBeginTime:-1,
                      courseEndTime:-1, salesNum:-1,
                    }]
                },
              ]
            }

            if (groupList !=undefined) {
              groupList[3] = groupList[3]
            } else {
              groupList[3] = [
                {
                  groupName:"", groupId: 1,
                  courseList:[
                    {
                      orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
                      coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
                      commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
                      enrollmentEndTime: -1, courseBeginTime:-1,
                      courseEndTime:-1, salesNum:-1,
                    }]
                },
              ]
            }


          }

        }

        groupList.map((item)=>{
          if (item == undefined) {
            // console.log("-------------item",item)
            item = {
              groupName:"", groupId: 1,
              courseList:[
                {
                  orgId:"云逸", courseImgName: "", weight:-1, courseId: -1,
                  coursePri:1, courseName: "", courseGroup:"", isFree: true, commonCash: -1,
                  commonYcoin: 1, discountCash: 0, discountYcoin: 0, enrollmentBeginTime: -1,
                  enrollmentEndTime: -1, courseBeginTime:-1,
                  courseEndTime:-1, salesNum:-1,
                }]
            }
            return item
          } else {
            // console.log("-------------item",item)
            return item
          }
        })
        // console.log("---------------groupList--",groupList)
        let swiperList = []
        for (let i = 0; i < 1;i++) {
          swiperList.push(groupList[i].courseList[0])
        }


        yield put({type:"getCourseInfoSave",payload:{groupList,newGroupList,swiperList,values}})
        // yield put({type:"newGroupListSave",payload:{groupList}})


      }
    }


  },
  subscriptions: {},
};
